// xc_lshop/savemoney/stroll/search.js
var smapidata = require('../model/smapi_data.js')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    sort:'1',
    history:{},
    keyword:""
  },
  xcloaddata: function (options, config) {
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#000000'
    })
    var that=this;
    var update = {};
    if (config && config.jtklmapi ) {
      if(config.jtklmapi.status==1){
        update['strollconfig'] = config.jtklmapi.stroll
        update['source']= options.source
        var windowWidth="";
      
        wx.getSystemInfo({
          success: (result) => {
            windowWidth=result['windowWidth']
          },
        })
        var width=(windowWidth - 20) * 0.485 
        width=width.toFixed(2)
        this.setData({
          itemwidth:width
        })
        this.setData(update)
          
        this.getnavdata()
      }else{
        this.error("功能已关闭",true)
      }
    }else{
      this.error("省钱未配制",true)
    }
    
  },
  clickitem(e){
    var dataset = e.currentTarget.dataset;
    var val=dataset.key
    this.setData({
      keyword:val
    })
    this.storage('add',val)
    this.storage('get')
    this.getsearchpagelist(this,1)
  },
  search(e){
    var key=e.detail
    this.setData({
      keyword:key
    })
    this.storage('add',key)
    this.storage('get')
    this.getsearchpagelist(this,1)

  },
  storage(op,keyword){
    var key="xcuid_"+this.data.member.id;
    var source=this.data.source
    switch(op){
      case "add":{
        var val=wx.getStorageSync(key);
        if(val){
          if(val[source]){
            var index = val[source].indexOf(keyword);
            if(index==-1){
              val[source].unshift(keyword) 
            } 
          }else{
            val[source]=[keyword]
          }
        }else{
          val={}
          val[source]=[keyword]
        }
        wx.setStorageSync(key, val)
        break;
      }
      case "clear":{
        wx.removeStorage({
          key: key,
        })
        break;
      }
      case "get":{
        var val=wx.getStorageSync(key);
        this.setData({
          history:val
        })
        break;
      }
    }
    
  },
  getnavdata(){
    var that=this;
    smapidata.getnavdata().then(function (ret){
      console.log("ret")
      console.log(ret)
      if(ret['status']=1){
        var update={
          platform:ret['data']
        }
        if(ret['data'][0]){
          update['source']=ret['data'][0]['source']
        }

        that.setData(update)
        // that.getsearchpagelist(that,1)
        // that.getclass(that.data.source)
        // that.gethotlist(that.data.source,20)
        // that.getpagelist(that,1)

      }else{
        var message=ret['message']?ret['message']:"获取失败";
        that.error(message,false)
      }
    })
    // that.getpagelist(that,1)

  },
  getsearchpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere={};
        strwhere['keyword']=this.data.keyword
        strwhere['source']=this.data.source
        strwhere['sort']=this.data.sort
      }
      this.setData({
        searchmodellist:[]
      })
    }
    smapidata.searchoffset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      console.log("this.data.searchmodellist")
      console.log(result)
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
         if (rest == 1) {
          updatemdoel["searchmodellist"] = result["obj"]["modellist"]['data'];
        } else {
          if (result["obj"]["modellist"]['data'].length > 0) {
            updatemdoel["searchmodellist"] = that.data.searchmodellist.concat(result["obj"]["modellist"]['data']);
          }
        }
      }     
      wx.stopPullDownRefresh();
      that.setData(updatemdoel);
      console.log("this.data.searchmodellist")
      console.log(that.data.searchmodellist)
    }, function () {
  
    });
  },
  afterlogin: function (options, config, userinfo) {
       this.setData({
         member:userinfo
       })
       this.storage('get')

  },
  clearkey(){
    this.setData({
      keyword:""
    })
  },
  clear(){
    this.storage('clear')
    wx.showToast({
      title: '清空成功',
    })
    this.storage('get')
    
  },
  onsourceChange(e){
    console.log(e)
    this.setData({
      source:e.detail.name
    })
    if(this.data.keyword){
      this.storage('add',this.data.keyword)
      this.storage('get')
      this.getsearchpagelist(this,1)
    }
  },
  onsortChange(e){
    this.setData({
      sort:e.detail.name
    })
    this.getsearchpagelist(this,1)
    // if(this.data.keyword){
    //   this.storage('add',this.data.keyword)
    //   this.storage('get')
    //   this.getsearchpagelist(this,1)

    // }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getsearchpagelist(this,-1)
  },


})